<?php
namespace app\appclass\controller;
use app\AdminRest;

use app\ApiRest;
use app\appclass\model\CardList;
use app\appclass\model\CardUser;
use app\appclass\model\CardWater;
use app\appclass\model\ClassCard;
use app\appclass\model\ClassCate;
use app\appclass\model\ClassCoach;
use app\appclass\model\ClassDate;
use app\appclass\model\ClassLable;
use app\appclass\model\ClassList;
use app\appclass\model\ClassRecord;
use app\appclass\model\ClassRoom;
use app\appclass\model\Lable;
use app\appclass\model\StoreInfo;
use app\card\model\Company;
use app\shop\model\IndexUser;
use app\shop\model\IndexUserInfo;
use think\App;
use think\facade\Db;


class IndexStaff extends ApiRest
{

    protected $model;

    protected $record_model;

    protected $lable_model;

    protected $room_model;

    protected $date_model;

    protected $store_id;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model       = new ClassList();

        $this->record_model= new ClassRecord();

        $this->lable_model = new ClassLable();

        $this->room_model  = new ClassRoom();

        $this->date_model  = new ClassDate();

        $this->store_id = $this->model->getStoreId($this->getUserId());

    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-31 16:14
     * @功能说明:教师未开课列表
     */
    public function coachClassNotStart(){
        //初始化
        $this->date_model->cancelfailClass($this->_uniacid);

        $input = $this->_param;

        $where[] = ['coach_id','=',$this->getUserId()];

        $where[] = ['start_time','>',time()];

        $data = $this->date_model->coachClassRecord($where,$input['page']);

        if(!empty($data['data'])){

            $cate_model = new ClassCate();

            foreach ($data['data'] as &$v){

                $v['day_text']  = date('Y-m-d',$v['start_time']);

                $v['week_text'] = changeWeek(date('w',$v['start_time']));

                $v['time_text'] = date('H:i',$v['start_time']).'-'.date('H:i',$v['end_time']);

                $v['cate_name'] = $cate_model->where(['id'=>$v['cate_id']])->value('title');

                $v['cate_type'] = $cate_model->where(['id'=>$v['cate_id']])->value('type');

            }
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-31 16:36
     * @功能说明:员工售卡列表
     */
    public function coachCardList(){

        $input = $this->_param;

        $card_user_model = new CardUser();

        $dis = [

            'adviser' => $this->getUserId()

        ];
        //总售卡金额
        $data['total_price'] = $card_user_model->getPrice($dis);

        $cancel_price = $card_user_model->saleOrCancelPrice($dis);

        $data['total_price'] = round($data['total_price']-$cancel_price,2);

        $where[] = ['a.adviser','=',$this->getUserId()];

        $where[] = ['a.is_cancel','=',0];

        if(isset($input['pay_type'])){

            $where[] = ['a.pay_type','=',$input['pay_type']];
        }

        if(isset($input['card_type'])){

            $where[] = ['a.card_type','=',$input['card_type']];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $satrt_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $where[] = ['a.create_time','between',"$satrt_time,$end_time"];

        }
        //列表
        $data['list'] = $card_user_model->dataList($where);

        if(!empty($data['list']['data'])){

            foreach ($data['list']['data'] as &$v){

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-31 17:15
     * @功能说明:教师上课统计
     */
    public function myClassStatistics(){

        $cate_model = new ClassCate();

        $dis = [

            'store_id' => $this->store_id,

            'status'   => 1
        ];

        $cate = $cate_model->where($dis)->order('id desc')->select()->toArray();

        if(!empty($cate)){

            foreach ($cate as &$value){
                //上课数量
                $value['class_count'] = $this->record_model->classCoachCount($value['type'],$this->getUserId(),$value['id']);

            }

        }

        return $this->success($cate);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-06-01 09:36
     * @功能说明:员工上过课的列表
     */
    public function myClassList(){

        $input = $this->_param;
        //我上过哪些课
        $have_class = $this->record_model->coachSuccessClass($this->getUserId());

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['id','in',$have_class];

        $dis[] = ['status','=',1];

        if(!empty($input['cate_id'])){

            $dis[] = ['cate_id','=',$input['cate_id']];
        }

        $class = $this->model->dataList($dis);

        $start_time = !empty($input['start_time'])?$input['start_time']:0;

        $end_time   = !empty($input['end_time'])?$input['end_time']:0;

        if(!empty($class['data'])){

            $cate_model = new ClassCate();

            foreach ($class['data'] as &$v){

                $cate_type = $cate_model->where(['id'=>$v['cate_id']])->value('type');

                $v['cate_type']   = $cate_type;
                //上课数量
                $v['class_count'] = $this->record_model->classCoachCount($cate_type,$this->getUserId(),0,$v['id'],$start_time,$end_time);
                //销售金额
                $v['sale_price']  = $this->record_model->classSalePrice($v['id'],$this->getUserId(),$start_time,$end_time);

            }

        }

        return $this->success($class);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-01 13:59
     * @功能说明:团课教师上课明细
     */
    public function publicCoachClassInfo(){

        $input = $this->_param;

        $dis[] = ['coach_id','=',$this->getUserId()];

        $dis[] = ['class_id','=',$input['class_id']];

        $dis[] = ['status','=',2];

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['start_time','between',"$start_time,$end_time"];

        }

        $data = $this->date_model->where($dis)->order('start_time,id')->paginate(10)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['sale_price'] = $this->record_model->classSalePrice($v['id'],$this->getUserId(),'','',2);

                $v['day_text']  = date('m-d',$v['start_time']);

                $v['year_text'] = date('Y',$v['start_time']);

                $v['time_text'] = date('H:i',$v['start_time']);
                //签到人数
                $v['sign_num']  = $this->record_model->where(['status'=>3,'class_date_id'=>$v['id']])->sum('num');

            }

        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-01 14:17
     * @功能说明:私有课教师上课明细
     */
    public function privateCoachClassInfo(){

        $input = $this->_param;

        $dis[] = ['coach_id','=',$this->getUserId()];

        $dis[] = ['class_id','=',$input['class_id']];

        $dis[] = ['status','=',3];

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['start_time','between',"$start_time,$end_time"];

        }

        $data = $this->record_model->staffIndexList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-02 13:46
     * @功能说明:预约详情
     */
    public function appClassInfo(){

        $input = $this->_param;
        //私教课
        if($input['cate_type']==1){

            $data = $this->record_model->dataInfo(['id'=>$input['id']]);

            $where = ['id'=>$input['id']];

        }else{
            //团课
            $data = $this->date_model->dataInfo(['id'=>$input['id']]);

            $where = ['class_date_id'=>$input['id']];

        }
        //预约人
        $list['user_list'] = $this->record_model->staffIndexList($where);
        //转时间
        $data['time_text'] = date('H:i',$data['start_time']).'-'.date('H:i',$data['end_time']);
        //教练名称
        $data['coach_name']= lbUserName($data['coach_id']);

        $where['status']   = 3;
        //签到人数
        $data['sign_num']  = $this->record_model->where($where)->sum('num');

        $list['class']     = $data;

        return $this->success($list);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-19 18:09
     * @功能说明:取消
     */
    public function cancel(){

        $input = $this->_input;

        $res   = $this->record_model->recordCancel($input['id'],$this->getUserId());

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-19 18:05
     * @功能说明:签到
     */
    public function sign(){

        $input = $this->_input;

        $data = $this->record_model->dataInfo(['id'=>$input['id']]);

        if(!empty($data['status'])!=1){

            $this->errorMsg('状态错误');
        }

        $res = $this->record_model->dataUpdate(['id'=>$input['id']],['status'=>3,'sign_user'=>$this->getUserId()]);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-02 16:53
     * @功能说明:帮用户预约团课
     */
    public function appClassDate(){

        $input = $this->_input;

        $coach_id = !empty($input['coach_id'])?$input['coach_id']:$this->getUserId();

        $res   = $this->record_model->appClassDate($input,$this->_uniacid,$input['user_id'],$coach_id);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-19 18:32
     * @功能说明:获取可以使用的会员卡
     */
    public function getCanUseCard(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        if(!empty($input['class_type'])){
            //时长
            $time_long = $this->model->where(['id'=>$input['id']])->value('time_long');

            $class_date['end_time'] = $input['start_time']+$time_long;

            $class_date['class_id'] = $input['id'];

            $class_date['start_time'] = $input['start_time'];

        }else{

            $class_date = $this->date_model->dataInfo($dis);

        }

        $class_card_model = new ClassCard();

        $data = $class_card_model->canUseCard($class_date,$input['user_id']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-02 17:11
     * @功能说明:搜索用户
     */

    public function userList(){

        $input = $this->_param;

        if(empty($input['name'])){

            return $this->success([]);
        }

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $where[] = ['a.nickName','like','%'.$input['name'].'%'];

//        $where[] = ['b.phone','like','%'.$input['name'].'%'];

        $user_model = new IndexUser();

        $data = $user_model->alias('a')
            ->join('longbing_card_user_phone b','a.id = b.user_id','left')
            ->where($dis)
            ->where(function ($query) use ($where){
                $query->whereOr($where);
            })
            ->field('a.id,a.nickName,b.phone,avatarUrl')
            ->group('a.id')
            ->select()
            ->toArray();


        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-04 16:41
     * @功能说明:课程排课列表
     */
    public function classDateList(){

        $input = $this->_param;

        $dis[] = ['store_id','<>',$this->store_id];

        if(!empty($input['cate_id'])){

            $dis[] = ['cate_id','=',$input['cate']];
        }

        if(!empty($input['day'])){

            $start_time = strtotime($input['day']);

            $end_time   = $start_time+86400-1;

            $dis[] = ['start_time','between',"$start_time,$end_time"];
        }

        $data = $this->date_model->where($dis)->order('start_time,id')->paginate(10)->toArray();

        if(!empty($data['data'])){

            $cate_model = new ClassCate();

            foreach ($data['data'] as &$v){

                $v['coach_name']   = lbUserName($v['coach_id']);

                $v['coach_avatar'] = lbUserAvatar($v['coach_id']);

                $v['cate_name'] = $cate_model->where(['id'=>$v['cate_id']])->value('title');

                $v['time_text'] = date('H:i',$v['satr_time']).'-'.date('H:i',$v['end_time']);

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-04 17:12
     * @功能说明:预约私交课
     */
    public function appPrivateClass(){

        $input = $this->_input;

        $coach_id = !empty($input['coach_id'])?$input['coach_id']:$this->getUserId();

        $res   = $this->record_model->appPrivateClass($input,$this->_uniacid,$input['user_id'],$coach_id);

        return $this->success($res);

    }










}
